package com.jasonli.service.impl;

import com.jasonli.dao.ProductInfoDao;
import com.jasonli.domain.PageBean;
import com.jasonli.domain.ProductInfo;
import com.jasonli.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoDao infoDao;

    public boolean save(ProductInfo info) {
        infoDao.save(info);
        return true;
    }

    public boolean delete(Integer pid) {
        infoDao.delete(pid);
        return true;
    }

    public boolean deleteByPids(List<Integer> pids) {
        for (Integer pid : pids) {
            infoDao.delete(pid);
        }
        return true;
    }

    public boolean update(ProductInfo info) {
        infoDao.update(info);
        return true;
    }

    public ProductInfo getByPid(Integer pid) {
        return infoDao.getByPid(pid);
    }

    public List<ProductInfo> getByTid(Integer tid) {
        return infoDao.getByTid(tid);
    }

    public List<ProductInfo> getAll() {
        return infoDao.getAll();
    }

    //分页查询
    public PageBean<ProductInfo> selectByPage(Integer currentPage, Integer pageSize) {
        Integer begin = (currentPage - 1) * pageSize;
        Integer size = pageSize;
        List<ProductInfo> rows = infoDao.selectByPage(begin, size);
        Integer totalCount = infoDao.selectTotalCount();
        PageBean<ProductInfo> pageBean = new PageBean<ProductInfo>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    public PageBean<ProductInfo> selectByPageAndCondition(Integer currentPage, Integer pageSize, ProductInfo info) {
        Integer begin = (currentPage - 1) * pageSize;
        Integer size = pageSize;

        String pname = info.getPname();
        if (pname != null && pname.length() > 0) {
            info.setPname("%"+pname+"%");
        }

        List<ProductInfo> rows = infoDao.selectByPageAndCondition(begin, size, info);
        Integer totalCount = infoDao.selectTotalCountByCondition(info);
        PageBean<ProductInfo> pageBean = new PageBean<ProductInfo>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

}
